<template>
  <div>
    <el-card style="margin: 20px 0px">
      <CategorySelect :show="scene != 0" @getCategoryId="getCategoryId"></CategorySelect>
    </el-card>
    <el-card>
      <div v-show="scene == 0">
        <el-button type="primary" icon="el-icon-plus" :disabled="!category3Id" @click="addSpu">添加spu</el-button>
        <el-table style="width: 100%" border :data="records">
          <el-table-column type="index" label="序号" width="80" align="center"></el-table-column>
          <el-table-column prop="spuName" label="spu名称" width=""></el-table-column>

          <el-table-column prop="description" label="spu描述" width=""></el-table-column>
          <el-table-column prop="" label="操作" width="" #default="{row}">
            <hint-button icon="el-icon-plus" size="mini" title="添加spu" type="success" @click="addSku(row)"
            ></hint-button>
            <hint-button type="warning" icon="el-icon-edit" size="mini" title="修改spu" @click="updateSpu(row)"
            ></hint-button>
            <hint-button icon="el-icon-info" size="mini" title="查看当前spu全部sku列表" type="info"
                         @click="handler(row)"
            ></hint-button>
            <el-popconfirm v-slot:reference title="这是一段内容确定删除吗？" @onConfirm="deleteSpu(row)">
              <hint-button icon="el-icon-delete" size="mini" title="删除spu" type="danger"
              ></hint-button>

            </el-popconfirm>
          </el-table-column>
        </el-table>
        <el-pagination
          style="margin-top:20px; text-align: center"
          :current-page="page"
          :total="total"
          :page-size="limit"
          :page-sizes=[3,5,10]
          @current-change="getSpuList"
          @size-change="handleSizeChange"
          layout="prev, pager, next, jumper, ->, sizes, total"
        >
        </el-pagination>
      </div>
      <SpuForm v-show="scene == 1" @changeScene="changeScene" ref="spuForm"/>
      <SkuForm v-show="scene == 2" ref="skuForm" @changeScene="changeScenes"/>
    </el-card>

    <el-dialog :before-close="close" :title="`${spu.spuName}`" :visible.sync="dialogTableVisible">
      <el-table v-loading="loading" :data="skuList" border>
        <el-table-column label="名称" property="skuName" width="150"></el-table-column>
        <el-table-column label="价格" property="price" width="200"></el-table-column>
        <el-table-column label="重量" property="weight"></el-table-column>
        <el-table-column #default="{row}" label="默认图片" property="">
          <img :src="row.skuDefaultImg" style="height: 100px; width: 100px">
        </el-table-column>
      </el-table>
    </el-dialog>
  </div>
</template>
<script>
import SpuForm from '@/views/product/Spu/SpuForm/index.vue'
import SkuForm from '@/views/product/Spu/SkuForm/index.vue'

export default {
  name: 'Spu',
  components: {
    SpuForm, SkuForm
  },
  data() {
    return {
      category1Id: '',
      category2Id: '',
      category3Id: '',
      page: 1,
      limit: 3,
      records: [],
      total: 0,
      scene: 0,
      dialogTableVisible: false,
      spu: {},
      skuList: [],
      loading: true
    }
  },
  methods: {
    getCategoryId({ category1Id, category2Id, category3Id }) {
      this.category1Id = category1Id
      this.category2Id = category2Id
      this.category3Id = category3Id
      this.getSpuList()
    },
    async getSpuList(pages = 1) {
      this.page = pages
      const { page, limit, category3Id } = this
      let result = await this.$API.spu.reqSpuList(page, limit, category3Id)
      console.log(result)
      if (result.code == 200) {
        this.total = result.data.total
        this.records = result.data.records
      }
    },
    handleSizeChange(limit) {
      this.limit = limit
      this.getSpuList()
    },
    addSpu() {
      this.scene = 1
      this.$refs.spuForm.addSpuData(this.category3Id)
    },
    updateSpu(row) {
      this.scene = 1
      this.$refs.spuForm.initSpuData(row)
    },
    changeScene({ scene, flag }) {
      this.scene = scene
      if (flag == '修改') {
        this.getSpuList(this.page)
      } else {
        this.getSpuList()
      }
    },
    async deleteSpu(row) {
      let result = await this.$API.spu.reqDeleteSpu(row.id)
      if (result.code == 200) {
        this.$message({ type: 'success', message: '删除成功' })
        this.getSpuList(this.records.length > 1 ? this.page : this.page - 1)
      }
    },
    addSku(row) {
      this.scene = 2
      this.$refs.skuForm.getData(this.category1Id, this.category2Id, row)
    },
    changeScenes(scene) {
      this.scene = scene
    },
    async handler(spu) {
      this.spu = spu
      this.dialogTableVisible = true

      let result = await this.$API.spu.reqSkuList(spu.id)
      if (result.code == 200) {
        this.skuList = result.data
        this.loading = false
      }
    },
    close(done) {
      done()
      this.skuList = []
      this.loading = true
    }
  }
}
</script>

<style scoped>

</style>
